package com.karaoke.android.kege.vm

import android.databinding.ObservableArrayList
import android.databinding.ObservableBoolean
import android.databinding.ObservableList
import android.os.Bundle
import android.view.View
import com.karaoke.android.data.LoadMoreEvent
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.domain.DataDictionary
import com.karaoke.android.data.domain.QSinger
import com.karaoke.android.data.domain.Singer
import com.karaoke.android.data.onIO
import com.karaoke.android.data.onUI
import com.karaoke.android.data.repository.SingerRepository
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.R
import com.karaoke.android.kege.adapter.SingerGridAdapter
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.mvvm.FragmentViewModel
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber

/**
 * Created by allen on 2017/4/20.
 */

class SingerPageViewModel() : FragmentViewModel()
{
    private val qSinger: QSinger = QSinger()

    val adapter = SingerGridAdapter<Singer>()
    val canLoadMore = ObservableBoolean()
    val showLoading = ObservableBoolean()
    val showEmpty = ObservableBoolean()
    val singers: ObservableList<Singer> = ObservableArrayList<Singer>()
    val singerBindings: ItemBinding<Singer> = ItemBinding.of<Singer>(BR.item, R.layout.singer_item)
    val loading = ObservableBoolean()
    var area: DataDictionary? = null

    init
    {
        EventBus.getDefault().register(this)
    }

    override fun onDestroy()
    {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onInitLoad()
    {
        super.onInitLoad()
        if (singers.isNotEmpty()) return
        showLoading.set(true)
        canLoadMore.set(true)
        qSinger.typeId = area?.id as Int

        SingerRepository.getSingers(qSinger, {
            singers.addAll(it.list)
            canLoadMore.set(it.hasNextPage)
            if (it.list.isEmpty())
                showEmpty.set(true)
            else
                showLoading.set(false)
        }, {})

//        NetworkTools.singerService.getSingers(qSinger).bindToLifecycle(this).onIO().onUI({
//                                                                                             singers.addAll(it.list)
//                                                                                             canLoadMore.set(it.hasNextPage)
//                                                                                             if (it.list.isEmpty())
//                                                                                                 showEmpty.set(true)
//                                                                                             else
//                                                                                                 showLoading.set(false)
//                                                                                         }, {})
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
    }

    @Subscriber
    fun onMoreAsked(e: LoadMoreEvent)
    {
        if (!loading.get() && canLoadMore.get())
        {
            loading.set(true)
            qSinger.page += 1
            SingerRepository.getSingers(qSinger, {
                singers.addAll(it.list)
                canLoadMore.set(it.hasNextPage)
                loading.set(false)
            }, { loading.set(false) })
        }
    }
}
